Skip to content

process and fs; Array flat/flatMap/toSpliced, Map.groupBy, String.fromCodePoint - #90

Merged
ASDAlexander77 merged 5 commits into
mainfrom
claude/funny-lamport-a3iphj
Nov 10, 2026
Merged

ASDAlexander77 merged 5 commits into
mainfrom
claude/funny-lamport-a3iphj

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Adds process and fs namespaces to src/generics/lib.generics.ts, over a new native wrapper src/wrappers/os.cpp, and a few missing Array, Map and String methods.

process

Works like Node's process, except that what Node has as properties are functions here, because a library namespace keeps no state set up at startup: argv(), getEnv(name) (undefined when unset), setEnv, unsetEnv, env() ([name, value] pairs), cwd(), chdir, pid(), platform(), exit(code).

  • argv comes from /proc/self/cmdline on Linux/Android and GetCommandLineW on Windows. Under the JIT it starts with tslang and its options, then the script, like node script.js.
  • On Windows the environment is read and written through the Win32 API, which every C runtime in the process shares.
  • env() returns pairs, not a Map. When process named Map<string, string> above the Map class, every program's own Map<string, string> lost its members.

fs

Synchronous, with Node's names: existsSync, statSync ({ isFile, isDirectory, size, mtimeMs }), readFileSync (a Uint8Array), readTextFileSync (a string), writeFileSync / writeTextFileSync, appendFileSync / appendTextFileSync, readdirSync (sorted), mkdirSync({ recursive }), rmSync({ recursive, force }), rmdirSync, renameSync, copyFileSync.

  • Errors are an Error with Node's message: ENOENT: no such file or directory, open 'a.txt'.
  • Paths are UTF-8 on every platform. On Windows they go through the wide API.

Array, Map, String

  • T[]: flat() (one level only), flatMap, toSpliced. Array<T>: flatMap, toSpliced. Both toSpliceds share one helper.
  • Map.groupBy(items, keySelector): groups in the order their keys first come. It doesn't work under -mm=own, where Map values can't be arrays, so its test is marked // not own.
  • String.fromCodePoint: a code point outside 0–0x10FFFF is a RangeError. fromCharCode's UTF-8 encoder moved into a helper that both use.
  • Set already had every ES2025 method, each with a test, so nothing changed there.

Build, tests, docs

  • os.cpp is added to scripts/build.sh, scripts/build_core.bat and scripts/build_android.bat. It links shell32 on Windows via #pragma comment.
  • New tests: process.ts, process_exit.ts, fs.ts, array_flat.ts, map_groupBy.ts, string_fromCodePoint.ts. The fs test works in __fs_test/, which is now in .gitignore.
  • New docs/process-fs.md and README entries.

Known limits

  • Files of 2 GB or more are an EFBIG error, because lengths are int, as in Stdio.
  • mtimeMs is only accurate to the second on Windows.
  • Under the JIT on Windows, exit() flushes only this library's C runtime, so text from print() that hasn't been flushed yet may be lost.
  • Not added: Array.isArray, because types are fixed at compile time.
  • Not added: String.raw. It needs two compiler changes:
    • A tagged template's tag gets only cooked strings. TemplateStringsArray is string[] with no .raw.
    • A tag's rest parameter isn't filled: arguments are matched to parameters one to one (mlirGen(TaggedTemplateExpression)).
  • Not fixed here: codePointAt returns one UTF-8 byte, not a code point. This was already the case before this PR.

Testing

On 13ad237 all four Ubuntu jobs (gc, rc, none, own) pass. The Windows jobs for that commit are still running.

🤖 Generated with Claude Code

https://claude.ai/code/session_01J8KRfWzDJu2qyU3x7oX6kv

process (Node's, with functions for its properties): argv, getEnv, setEnv,
unsetEnv, env, cwd, chdir, pid, platform, exit.

fs, synchronous, by Node's names: existsSync, statSync, readFileSync,
readTextFileSync, writeFileSync, writeTextFileSync, appendFileSync,
appendTextFileSync, readdirSync, mkdirSync, rmSync, rmdirSync, renameSync,
copyFileSync. Errors carry Node's message ("ENOENT: ..., open 'a.txt'").

Both are namespaces in lib.generics.ts over a new wrapper, os.cpp, which every
build script now compiles. UTF-8 paths, the wide API on Windows. Tests:
process.ts, process_exit.ts, fs.ts; docs/process-fs.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfWzDJu2qyU3x7oX6kv
- __os_text: a u8 buffer ended with 0; a char can't take null in the
  generics' strict null mode, which failed every program.
- The helpers take their type parameter from their argument, as sleep does.
- process.env() gives [name, value] pairs: naming Map<string, string> above
  the Map class made that type before Map was declared, and every program's
  own Map<string, string> then had no members (map_get, map_set, ... failed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfWzDJu2qyU3x7oX6kv
A bare array literal pushed onto [string, string][] was a string[], which
can't be cast to the pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfWzDJu2qyU3x7oX6kv
- T[]: flat() (one level), flatMap, toSpliced; Array<T>: flatMap, toSpliced.
  toSpliced shares one helper, __toSpliced, between the two.
- Map.groupBy(items, keySelector): each group made whole before it goes in.
  Not under -mm=own, whose Map values can't be arrays (the test says so).
- String.fromCodePoint, with fromCharCode's UTF-8 encoder moved out into
  utf8Push for both.

Tests: array_flat.ts, map_groupBy.ts, string_fromCodePoint.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfWzDJu2qyU3x7oX6kv
@ASDAlexander77 ASDAlexander77 changed the title process and fs: the command line, the environment and the file system process and fs; Array flat/flatMap/toSpliced, Map.groupBy, String.fromCodePoint Nov 10, 2026
…ring keys

array_flat: a literal [1, 2, 3] is an s32[], which a callback taking a number
did not match. map_groupBy: keys from substring, not item[0], and each group
printed, so a wrong one shows what it held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J8KRfWzDJu2qyU3x7oX6kv
@ASDAlexander77
ASDAlexander77 merged commit 9e93e12 into main Nov 10, 2026
5 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants